package com.neusoft.neuiotms.dcvm.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.codahale.metrics.annotation.Timed;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcvm.dto.ModelDTO;
import com.neusoft.neuiotms.dcvm.model.Model;
import com.neusoft.neuiotms.dcvm.model.ModelVersion;
import com.neusoft.neuiotms.dcvm.repository.ModelRepository;
import com.neusoft.neuiotms.dcvm.repository.ModelVersionRepository;
import com.neusoft.neuiotms.dcvm.service.FileService;
import com.neusoft.neuiotms.dcvm.service.ModelService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("模具管理")
@RestController
@RequestMapping(value = "/api/w/models")
public class ModelController {

    @Autowired
    private ModelService modelService;
    @Autowired
    private ModelRepository modelRepository;
    @Autowired
    private ModelVersionRepository modelVersionRepository;
    @Autowired
    private FileService fileService;

    @ApiOperation("查询所有模具")
    @GetMapping
    public ResultListDTO<ModelDTO> index() {
        final List<ModelDTO> allModels = this.modelService.findAll();
        return ResultListDTO.success(allModels);
    }

    @ApiOperation("分页查询所有模具")
    @GetMapping("/p")
    public PageResultDTO<ModelDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.modelService.search(pageable, searchable));
    }

    @ApiOperation("模具详情")
    @GetMapping("/{id}")
    public ResultDTO<ModelDTO> details(@PathVariable final Long id) {
        final ModelDTO dto = this.modelService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("创建模具")
    @PostMapping
    public ResultDTO<ModelDTO> create(@RequestBody final ModelDTO dto) {
        final ModelDTO result = this.modelService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("修改模具")
    @PutMapping("/{id}")
    public ResultDTO<ModelDTO> update(@PathVariable final Long id,
            @RequestBody final ModelDTO dto) {
        final ModelDTO result = this.modelService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("删除模具")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.modelService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("下载3D文件")
    @GetMapping("/3d/{id}/{version}")
    public void _3ddownload(@PathVariable final String id, @PathVariable final String version,
            final HttpServletResponse response) {
        Model model = null;
        if (NumberUtils.isCreatable(id)) {
            model = this.modelRepository.getOne(Long.valueOf(id));
        } else {
            model = this.modelRepository.findByModelCode(id);
        }
        if (null == model) {
            throw new UnitedException("未找到模具");
        }
        final ModelVersion mv = this.modelVersionRepository.findOneByModelAndVersionCode(model, version);
        this.fileService.download(mv.getFileUrl(), response);
    }

    @ApiOperation("下载预览文件")
    @GetMapping("/preview/{id}")
    public void previewDownload(@PathVariable final String id, final HttpServletResponse response) {
        Model model = null;
        if (NumberUtils.isCreatable(id)) {
            model = this.modelRepository.getOne(Long.valueOf(id));
        } else {
            model = this.modelRepository.findByModelCode(id);
        }
        if (null == model) {
            throw new UnitedException("未找到模具");
        }
        this.fileService.download(model.getPreviewFsId(), response);
    }

    @ApiOperation("下载小地图文件")
    @GetMapping("/map/{id}")
    public void mapDownload(@PathVariable final String id, final HttpServletResponse response) {
        Model model = null;
        if (NumberUtils.isCreatable(id)) {
            model = this.modelRepository.getOne(Long.valueOf(id));
        } else {
            model = this.modelRepository.findByModelCode(id);
        }
        if (null == model) {
            throw new UnitedException("未找到模具");
        }
        this.fileService.download(model.getMapFsId(), response);
    }

    @ApiOperation("上传模具包")
    @Timed
    @PostMapping("/upload")
    public ResultDTO<?> upload(@RequestParam("file") final MultipartFile file) {
        final int count = this.modelService.upload(file);
        return ResultDTO.success(count);
    }

}
